#include "ColorPanel.h"

#include <utility>

ColorPanel::ColorPanel(QColor initColor, QWidget *parent) : QWidget(parent)
{
    color = std::move(initColor);
    title = new QLabel(this);
    title -> setText("调色盘");

    r = new SpinBoxWithSlider("红", 0, 0, 255, this);
    g = new SpinBoxWithSlider("绿", 0, 0, 255, this);
    b = new SpinBoxWithSlider("蓝", 0, 0, 255, this);

    r -> SetValue(color.red());
    g -> SetValue(color.green());
    b -> SetValue(color.blue());

    layout = new QVBoxLayout(this);
    layout -> addWidget(title);
    title -> setAlignment(Qt::AlignmentFlag::AlignCenter);
    layout -> addWidget(r);
    layout -> addWidget(g);
    layout -> addWidget(b);
    layout -> setAlignment(Qt::AlignmentFlag::AlignCenter);
    setLayout(layout);

	r -> setContentsMargins(0, 0, 0, 0);
	g -> setContentsMargins(0, 0, 0, 0);
	b -> setContentsMargins(0, 0, 0, 0);
	title -> setContentsMargins(0, 0, 0, 0);
	setContentsMargins(0, 0, 0, 0);
	layout -> setAlignment(Qt::AlignmentFlag::AlignCenter);

    connect(r, SIGNAL(ValueChanged(int)), this, SLOT(ValueChangeHandle(int)));
    connect(g, SIGNAL(ValueChanged(int)), this, SLOT(ValueChangeHandle(int)));
    connect(b, SIGNAL(ValueChanged(int)), this, SLOT(ValueChangeHandle(int)));
}

ColorPanel::~ColorPanel()
{
    delete title;
    delete r;
    delete g;
    delete b;
    delete layout;
}

void ColorPanel::SetValue(const QColor &c)
{
    color = c;
    r -> SetValue(color.red());
    g -> SetValue(color.green());
    b -> SetValue(color.blue());
    emit(ValueChanged(color));
}

void ColorPanel::ValueChangeHandle(int v)
{
    color.setRed(r -> GetValue());
    color.setBlue(b -> GetValue());
    color.setGreen(g -> GetValue());
    emit(ValueChanged(color));
}

void ColorPanel::SetTitle(const QString &t)
{
    title -> setText(t);
}
